package com.atguigu.java;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

/*
 * 
 * 抽象基类			节点流										缓冲流(处理流的一种):提高数据传输的速度
 * InputStream 		FileInputStream(read(byte[] buffer)			BufferedInputStream(read(byte[] buffer)	
 * OutputStream		FileOutputStream(write(byte[] buffer,0,len)	BufferedOutputStream(write(byte[] buffer,0,len)
 * Reader			FileReader(read(char[] buffer)				BufferedReader(read(char[] buffer / readLine())	
 * Writer			FileWriter(write(char[] buffer,0,len)		BufferedWriter(write(char[] buffer,0,len)
 * 
 * 
 * Stream 字节
 * Reader/ Writer 字符
 */
public class BufferedTest {
	
	//综合使用BufferedReader和BufferedWriter的例子
	@Test
	public void testBufferedReaderWriter() throws Exception{
		//1.
		File file1 = new File("dbcp.txt");
		File file2 = new File("dbcp2.txt");
		//2.1
		FileReader fr = new FileReader(file1);
		FileWriter fw = new FileWriter(file2);
		//2.2
		BufferedReader br = new BufferedReader(fr);
		BufferedWriter bw = new BufferedWriter(fw);
		
		//3.使用String
		String str;
		while((str = br.readLine()) != null){//读入到String中的数据不包含换行符
			bw.write(str);
			bw.write("\n");
//			bw.newLine();
//			bw.flush();//刷新数据
		}
		
		
		//4.
		br.close();
		bw.close();
	}
	
	//单独使用BufferedReader的例子
	@Test
	public void testBufferedReader() throws Exception{
		//1.
		File file = new File("dbcp.txt");
		//2.1
		FileReader fr = new FileReader(file);
		//2.2
		BufferedReader br = new BufferedReader(fr);
		//3.方式一：使用char[]
//		char[] cbuf = new char[20];
//		int len;
//		while((len = br.read(cbuf)) != -1){
//			String str = new String(cbuf,0,len);
//			System.out.print(str);
//		}
		
		//3.方式二：使用String
		String str;
		while((str = br.readLine()) != null){//读入到String中的数据不包含换行符
			System.out.println(str);
		}
		
		
		//4.
		br.close();
	}

	@Test
	public void testCostTime() {
		long start = System.currentTimeMillis();

		String src = "C:\\Users\\Administrator\\Desktop\\1.avi";
		String dest = "C:\\Users\\Administrator\\Desktop\\3.avi";
//		copyWithFile(src, dest);//5750
		copyWithBuffered(src, dest);//206

		long end = System.currentTimeMillis();

		System.out.println("花费的时间为：" + (end - start));

	}

	// 使用BufferedInputStream/BufferedOutputStream实现文件的复制
	public void copyWithBuffered(String src, String dest) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//1.提供原文件和目标文件
			File srcFile = new File(src);
			File destFile = new File(dest);
			
			//2.创建相应的输入流和输出流
			FileInputStream fis = new FileInputStream(srcFile);
			FileOutputStream fos = new FileOutputStream(destFile);
			
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			
			//3.读取数据并写出数据的操作
			byte[] buffer = new byte[1024];
			int len;//记录读入字节数组中的字节数
			while((len = bis.read(buffer)) != -1){
				
				bos.write(buffer, 0, len);
				bos.flush();//刷新数据
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//4.关闭资源
			if(bos != null){
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
			
		}
	}

	// 使用FileInputStream/FileOutputStream实现文件的复制
	public void copyWithFile(String src, String dest) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			//1.提供原文件和目标文件
			File srcFile = new File(src);
			File destFile = new File(dest);
			
			//2.创建相应的输入流和输出流
			fis = new FileInputStream(srcFile);
			fos = new FileOutputStream(destFile);
			
			//3.读取数据并写出数据的操作
			byte[] buffer = new byte[100];
			int len;//记录读入字节数组中的字节数
			while((len = fis.read(buffer)) != -1){
				
				fos.write(buffer, 0, len);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//4.关闭资源
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
			
		}
	}

	@Test
	public void test1() throws Exception {

		// 1.创建文件
		File src = new File("minghui.jpg");
		File dest = new File("minghui2.jpg");

		// 2.1 创建节点流
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(dest);
		// 2.2 创建缓冲流
		BufferedInputStream bis = new BufferedInputStream(fis);
		BufferedOutputStream bos = new BufferedOutputStream(fos);

		// 3.读写操作
		byte[] buffer = new byte[100];
		int len;
		while ((len = bis.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}

		// 4.关闭资源：（从外往里关闭）
		bos.close();
		bis.close();
		// 可以省略
		// fos.close();
		// fis.close();

	}
}
